//
//  PhoneCodeView.m
//  ZRwalletForMerchant
//
//  Created by 文彬 on 15/7/18.
//  Copyright (c) 2015年 文彬. All rights reserved.
//

#import "PhoneCodeButton.h"
//#import "CommonRequest.h"

#define maxCount  60 //倒计时秒数

@interface PhoneCodeButton ()
{
    NSTimer *timer;
    int num;
}

@end

@implementation PhoneCodeButton

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)dealloc
{
    NSLog(@"codedealloc");
    if ([timer isValid])
    {
        [timer invalidate];
        timer = nil;
    }
}

- (id)init
{
    self = [super init];
    if (self)
    {
         [self initControl];
    }
    return self;
}

- (void)awakeFromNib{
    [super awakeFromNib];
    
    [self initControl];
}

- (void)initControl{
    
    self.backgroundColor = [UIColor whiteColor];
    
    self.titleLabel.font = [UIFont systemFontOfSize:13];
    
    //        [self setBackgroundImage:[UIImage imageNamed:@"button_small"] forState:UIControlStateNormal];
    [self setTitleColor:kTitleColor forState:UIControlStateNormal];
    [self setTitle:@"短信验证" forState:UIControlStateNormal];
    [self addTarget:self action:@selector(getMessageCode) forControlEvents:UIControlEventTouchUpInside];
}

- (void)beginTimer
{
    
//    [self setBackgroundImage:[UIImage imageNamed:@"ip_shszmm_ob"] forState:UIControlStateNormal];
    [self setTitle:[NSString stringWithFormat:@"%d秒后重试",maxCount] forState:UIControlStateNormal];
      [self setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.userInteractionEnabled = NO;
    if ([timer isValid])
    {
        [timer invalidate];
        timer = nil;
    }
    num = maxCount;
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updataNumber) userInfo:nil repeats:YES];
    
    //不加此行代码时  按住页面时 主线程阻塞 计时器将停止
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

/**
 *  获取验证码
 */
- (void)getMessageCode
{
    if (self.getCode!=nil)
    {
        self.getCode();
    }
    
    if(self.phoneNum==nil||[self.phoneNum isEqualToString:@""])
    {
        [SVProgressHUD showErrorWithStatus:@"请输入手机号"];
        return;
    }
    
     [self beginTimer];
//    [CommonRequest getMesscodeTokenWithType:self.opeType phone:self.phoneNum Success:^(id result) {
//
//        [SVProgressHUD showSuccessWithStatus:@"发送成功，请注意查收！"];
//
//        [self beginTimer];
//
//    } fail:^{
//
//
//    }];
}

/**
 *  更新计时器显示
 */
-(void)updataNumber
{
    num--;
    if (num==0)
    {
//        [self setBackgroundImage:[UIImage imageNamed:@"button_small"] forState:UIControlStateNormal];
          [self setTitleColor:kTitleColor forState:UIControlStateNormal];
        [self setTitle:@"重新获取" forState:UIControlStateNormal];
        self.userInteractionEnabled = YES;
        
        [timer invalidate];
        timer = nil;
    }
    else
    {
        [self setTitle:[NSString stringWithFormat:@"%d秒后重试",num] forState:UIControlStateNormal];
    }
}

@end
